"ui";

var config = storages.create("auto_reader_config");
if (!config.contains("text1")) {
  toastLog("使用默认配置");
  // 默认执行配置
  var default_conf = {
      text1: "", 
      input1:"",
      ck1:"",
      ck2:"",
      img1:"https://www.baidu.com/img/bd_logo1.png",
      radiogroup1:""
  };
  // 储存默认配置到本地
  Object.keys(default_conf).forEach(function(key) {
    config.put(key, default_conf[key]);
  });
}


function draw_view() {
    ui.layout(
      <ScrollView>
        <vertical id="viewport">
        // viewport START     
          <frame>
            <appbar>
              <toolbar id="toolbar" title="控件示例" />
            </appbar>
          </frame>
          
         <vertical w="*" gravity="left" layout_gravity="left" margin="10">
          <text text="运行时长(分钟)：" textColor="#666666" textSize="14sp" />
          <spinner id="run_time_sp" entries="1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60"/>
        </vertical>
        <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>
    
          <vertical w="*" gravity="left" layout_gravity="relative" margin="10">
            <text text="文本控件：" textColor="#666666" textSize="14sp" />
            <text id="text1" text="超大字体" textColor="#666666" textSize="40sp" />
          </vertical>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

          <vertical w="*" gravity="left" layout_gravity="relative" margin="10">
            <text text="输入框控件：" textColor="#666666" textSize="14sp" />
            <input id="input1" inputType="text" text="" />
          </vertical>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

          <vertical w="*" gravity="left" layout_gravity="relative" margin="10">
            <text text="复选框控件：" textColor="#666666" textSize="14sp" />
            <checkbox id="ck1" text="选项1" checked="true" />
            <checkbox id="ck2" text="选项2" checked="" />
          </vertical>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

          <vertical w="*" gravity="center" layout_gravity="relative" margin="10">
            <text text="图片控件：" textColor="#666666" textSize="14sp" />
            <img id="img1" w="200" h="50" src="https://www.baidu.com/img/bd_logo1.png" />
          </vertical>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

          <vertical w="*" gravity="left" layout_gravity="left" margin="10">
            <text text="单选框控件：" textColor="#666666" textSize="14sp" />
            <radiogroup id="radiogroup1" orientation="horizontal" margin="0 10">
              <radio text="是" checked="true" />
              <radio text="否" checked="false" marginLeft="20" />
            </radiogroup>
          </vertical>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

          <horizontal w="*" gravity="center" layout_gravity="relative" margin="10">
              <button id="ok_btn" text="确定"/>
              <button id="cancel_btn" text="取消"/>
          </horizontal>
          <horizontal w="*" h="1sp" bg="#cccccc" margin="10 0"></horizontal>

        // viewport END  
        </vertical>
      </ScrollView>
    );

    // 更新本地配置同时重绘UI
    function update(target, new_val) {
      config.put(target, new_val);
    }

    // 格式化
    function format(val) {
      return val.toString();
    }

    // 点击事件
    ui.text1.on("click",function(e){
      toastLog(e.getText());
    });

    // 单选框事件
    ui.radiogroup1.setOnCheckedChangeListener(function(radioGroup, id) {
      // let index = (id + 1) % radioGroup.getChildCount();
      // toast(radioGroup.getChildAt(index).getText());
      // if (radioGroup.getChildAt(index).getText() == "是") {
      //   toastLog(true);
      // } else {
      //   toastLog(false);
      // }
    });

    // 更新所有配置
    ui.emitter.on("pause", () => {
      // toastLog(ui.input1.getText());
    });

    // 确定按钮
    ui.ok_btn.on("click",function(e){
      //文件路径
      var path = "/sdcard/auto_reader_config.txt";
      //要写入的文件内容
      let radiogroup1 = false;
      if(ui.radiogroup1.getChildAt(0).checked){1
        radiogroup1 = true;
      }else{
        radiogroup1 = false;
      }
      let default_conf = {
          text1: format(ui.text1.getText()), 
          input1:format(ui.input1.getText()),
          ck1:format(ui.ck1.checked),
          ck2:format(ui.ck2.checked),
          radiogroup1:format(radiogroup1)
      }
      let text = JSON.stringify(default_conf);
      //以写入模式打开文件
      var file = open(path, "w");
      //写入文件
      file.write(text);
      //关闭文件
      file.close();
      toastLog("已保存");
      // 关闭窗体
      activity.finish();
    });

    // 取消按钮
    ui.cancel_btn.on("click",function(e){
      // toastLog(e.getText());
      // 关闭窗体
      activity.finish();
    });

}

draw_view();